package urjc.as.emergencies.client.jpanels.missions.ambulances;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;

import org.restlet.data.Response;
import org.restlet.data.Status;

import net.miginfocom.swing.MigLayout;
import urjc.as.emergencies.client.CommToolkit;
import urjc.as.emergencies.client.DynamicTree;
import urjc.as.emergencies.client.Node;
import urjc.as.emergencies.client.TextArea;
import urjc.as.emergencies.data.Ambulance;

@SuppressWarnings("serial")
public class PanelAmbulances extends JPanel {
	private TextArea text;
	private JTree tree;
	private JComboBox comboBoxAmbulanceType;
	private DynamicTree dynTree;

	/**
	 * Create the panel.
	 */
	public PanelAmbulances(TextArea textPanel, JTree jtree, DynamicTree _tree) {
		this.text =  textPanel;
		this.tree = jtree;
		this.dynTree = _tree;
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Misiones", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLUE));
		setLayout(new MigLayout("", "[][grow]", "[][][][][][]"));
		
		JLabel lblNewLabel = new JLabel("Tipo Ambulancia");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblNewLabel, "cell 0 2,alignx trailing");
		
		comboBoxAmbulanceType = new JComboBox();
		comboBoxAmbulanceType.setModel(new DefaultComboBoxModel(new String[] {"UVI Movil", "UAD", "Ambulancia Normal"}));
		add(comboBoxAmbulanceType, "cell 1 2,growx");
		
		JButton ButtonCrearAmbulancia = new JButton("Crear Ambulancia");
		ButtonCrearAmbulancia.setToolTipText("Haga click para dar de alta una ambulancia");
		ButtonCrearAmbulancia.setMnemonic('m');
		ButtonCrearAmbulancia.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				
				if (node != null) {
					// Obtenemos la URI asociada al nodo seleccionado
					Node nodeInfo = (Node) node.getUserObject();
					String targetURI = nodeInfo.getUri();
					
					// Creamos una ambulancia a partir de los datos
					// introducidos
					Ambulance amb = new Ambulance();
					amb.setStatus(Ambulance.LIBRE);
					amb.setType(comboBoxAmbulanceType.getSelectedItem().toString());
					
					// Hacemos la petición para crear un nuevo mailbox de
					// usuario, sobre la URL adecuada
					Response resp = CommToolkit.createAmbulance(amb, targetURI);
					
					if (resp.getStatus().equals(Status.SUCCESS_CREATED)) {
						text.append(resp.getLocationRef().toString());
						// Repintamos la interfaz
						dynTree.redraw(resp.getLocationRef().toString(), Node.AMBULANCE);
					} else {
						text.append(resp.getStatus().getDescription());
					}
		
					
					
				}
			}
		});
		add(ButtonCrearAmbulancia, "cell 0 5 2 1,alignx center");

	}

}
